function [Dpi,Dp,p_ij] = Dprofits_Ds(q_ij,beta,delta,s_i,cobb_par,E,M,r,K_obs,c_inv,cu,sigma,W_k,U_k,J_k,sf)
% Here we compute the derivative of the platform's profits with respect to s_i
% See Appendix C.4 for a derivation.

%--------------- Compute prices p_ij and entrants that ensure that exactly 
%                q_ij exporters demand transportation on route ij

etilde_ij = entrants(q_ij,s_i,cobb_par,M,delta,sf);
Gunc = etilde_ij./(E - nansum(etilde_ij,2));
e_i = inverse_m(nansum(q_ij,2),s_i,cobb_par,M,sf);
lambdae_i = nansum(q_ij,2)./e_i;
p_ij = prices(q_ij,beta,delta,s_i,cobb_par,E,M,r,K_obs,c_inv,sf);

%--------------------- derivative ships' meeting rate is set to zero, since
% in our data ships are the long side of the amrket

Dl = zeros(M,1);

%--------------------- derivatives exporters e_i searching
De = zeros(M,M);
for i = 1:M
    q_i = nansum(q_ij(i,:),2);
    A = cobb_par(i,1);
    a = cobb_par(i,2);
    for j = 1:M
        Dei_Dsi = (1-delta)./(A.^(1./a));
        Dei_Dsi = Dei_Dsi.*(1-a)./a;
        Dei_Dsi = Dei_Dsi.*(q_i.^((1-a)./a));
        Dei_Dsi = Dei_Dsi.*q_ij(i,j);
        Dei_Dsi = Dei_Dsi.*(s_i(i).^(-1/a));
        De(i,j) = -Dei_Dsi;
    end
end

%--------------------- derivatives entry probabilities

DG = zeros(M,M);
for i = 1:M
    for j = 1:M
        DG(i,j) = De(i,j) + nansum(De(i,:)).*Gunc(i,j);
        DG(i,j) = DG(i,j)./etilde_ij(i,j);
    end
end

%--------------------- derivatives prices

Dp = zeros(M,M);

for i = 1:M
    l = lambdae_i(i);
    for j = 1:M
        Dpij_Dsi = -DG(i,j).*(1-delta*beta*(1-l))./l;
        Dpij_Dsi = Dpij_Dsi + (log(Gunc(i,j))+K_obs(i,j)).*((1-delta.*beta)./(l.^2)).*Dl(i);
        Dpij_Dsi = Dpij_Dsi + c_inv(i).*Dl(i)./(l.^2);
        Dp(i,j) = Dpij_Dsi;
    end
end


%--------------- derivative profits

Dpi = zeros(M,1);


for i = 1:M
    Dpi(i) =  - (cu(i)./sigma) + (J_k(i)- U_k(i))./sigma;
end

end

